import React, { PureComponent } from 'react';
import { message, Form, Input, Divider } from 'antd';
import { connect } from 'dva';
import SyOrderTableComponent from '../../components/SyOrderTableComponent';
import moment from 'moment';
import { getAuthority, setAuthority } from '../../utils/authority';
import styles from '../../components/SyOrderTableComponent/index.less';

const FormItem = Form.Item;
const { TextArea } = Input;

@Form.create()
@connect(state => ({
  modelState: state.productCategory,
}))
export default class Product extends SyOrderTableComponent {

  // 缺省
  _initSortion = { sortField: 'id', sortOrder: 'desc', };
  _initPagination = { pageSize: 10, current: 1, };
  _initCriteriaQuery = {};
  _operator = [
    {
      needForm: false,
      needConfirm: true,
      buttonLabel: '上架 / 下架',
      // okType: 'danger',
      buttonDisabled: 'none',
      confirmTitle: '确认 上架 / 下架？',
      handleEvent: () => {

        let needOnShelf = this.state.selectedRows.filter(entity => entity.state == 0).map(entity => entity.id);
        let needOffShelf = this.state.selectedRows.filter(entity => entity.state == 1).map(entity => entity.id);

        const { dispatch } = this.props;
        if (needOnShelf && needOnShelf.length > 0) {
          dispatch({
            type: this.state.baseModel + '/onShelf',
            payload: {
              ids: needOnShelf
            },
            callback: (code, msg) => {
              if (code == 200) {
                message.success(msg);
                this.resetCurrentForm();
                dispatch({
                  type: this.state.baseModel + '/fetch',
                  payload: {
                    ...this.state.initPagination,
                    ...this.state.initSortion,
                    ...this.state.initCriteriaQuery,
                    ...this.state.statusQuery,
                  }
                });
              } else {
                message.error(msg);
              }
            },
          });
        }

        if (needOffShelf && needOffShelf.length > 0) {
          dispatch({
            type: this.state.baseModel + '/offShelf',
            payload: {
              ids: needOffShelf
            },
            callback: (code, msg) => {
              if (code == 200) {
                message.success(msg);
                this.resetCurrentForm();
                dispatch({
                  type: this.state.baseModel + '/fetch',
                  payload: {
                    ...this.state.initPagination,
                    ...this.state.initSortion,
                    ...this.state.initCriteriaQuery,
                    ...this.state.statusQuery,
                  }
                });
              } else {
                message.error(msg);
              }
            },
          });
        }


      },
    }

  ];

  initSelection() {
    const { dispatch } = this.props;
    // dispatch({
    //   type: 'systemUser/fetchSelectItems',
    //   payload: { apiUrl: '/system/organization', selectName: 'systemOrganizationSelectItems' }
    // });
    // dispatch({
    //   type: 'systemUser/fetchSelectItems',
    //   payload: { apiUrl: '/system/role', selectName: 'systemRoleSelectItems' }
    // });
  }


  componentWillMount() {
    this.initSelection();
    this.setState({
      baseModel: 'productCategory',
      operator: this._operator,
      initPagination: this._initPagination,
      needTableAlert: true,
      needSearch: true,
      needAdd: true,
      needEdit: true,
      needDelete: true,
      needTableColumnsOperator: true,
      needTableCheckbox: true,
      tableBorder: false,
      tableScroll: { y: 800 },
      tableColumns: [
        {
          title: 'ID',
          dataIndex: 'id',
          width: 170,
        },
        {
          title: '上架状态',
          dataIndex: 'state',
          width: 170,
          render: (text, record) => {
            if ((record && record.state == 1)) {
              return (<span> 是 </span>);
            }
            return (<span style={{ color: '#e8e8e8' }}> / </span>);
          },
        },
        {
          title: '分组名称',
          dataIndex: 'name',
          width: 170,
        },
        {
          title: '分组ID',
          dataIndex: 'catId',
          width: 170,
        },
        {
          title: '分组级别',
          dataIndex: 'catClass',
          width: 170,
        },
        {
          title: '上级分组',
          dataIndex: 'parentId',
          width: 170,
        },
        {
          title: '商品数',
          dataIndex: 'goodsCount',
          width: 170,
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          // width: 180,
        },
      ],
      searchFormItem: [
        {
          name: 'name',
          label: '分组名称',
          placeholder: '分组名称',
          type: 'string',
        },
        {
          name: 'parentId',
          label: '上级分组ID',
          placeholder: '上级分组ID',
          type: 'string',
        },
        {
          name: 'catId',
          label: '当前分组ID',
          placeholder: '当前分组ID',
          type: 'string',
        },
      ],
      simpleAddFormItem: [
        {
          name: 'name',
          label: '分组名称',
          placeholder: '分组名称',
          type: 'string',
          rules: [{
            required: true, message: '请输入分组名称',
          }],
        },
        {
          name: 'parentId',
          label: '上级分组ID',
          placeholder: '上级分组ID',
          type: 'string',
          rules: [{
            required: true, message: '请输入上级分组ID',
          }],
        },
        {
          name: 'catId',
          label: '当前分组ID',
          placeholder: '当前分组ID',
          type: 'string',
          rules: [{
            required: true, message: '请输入当前分组ID',
          }],
        },
      ],
      simpleEditFormItem: [
        {
          name: 'name',
          label: '分组名称',
          placeholder: '分组名称',
          type: 'string',
          rules: [{
            required: true, message: '请输入分组名称',
          }],
        },
        {
          name: 'parentId',
          label: '上级分组ID',
          placeholder: '上级分组ID',
          type: 'string',
          rules: [{
            required: true, message: '请输入上级分组ID',
          }],
        },
        {
          name: 'catId',
          label: '当前分组ID',
          placeholder: '当前分组ID',
          type: 'string',
          rules: [{
            required: true, message: '请输入当前分组ID',
          }],
        },
      ],
    });
  }

  constructor(props) {
    super(props);
  }

  componentDidMount() {
    super.componentDidMount();
  }

}
